<?php
namespace App\HttpController\Api;


use App\Repositories\Eloquent\Api\MemberInfoModel;

class Member extends Base  {

    protected $_model;


    /**
     * @Api(name="用户注册登录",group="用户",path="api/member/register",description="登录注册是一个接口")
     * @Method(allow={POST})
     * @Param(name="type",required="true",description="0 游客 1 QQ 2 微信 3 系统添加 4 手机号注册")
     * @Param(name="openid",required="true",description="第三方唯一表示")
     * @Param(name="name",required="true",description="昵称")
     * @Param(name="heading",required="true",description="头像")
     * @Param(name="sex",required="true",description="性别")

     * @ApiSuccess(
    {
    "code": 200,
    "msg": "OK",
    "count": 0,
    "data": {
    "login_key": "8bf85ee913b304e43b41"
    }
    }
    )
     * @ResponseParam(name="code",description="状态码")
     * @ResponseParam(name="count",description="条数")
     * @ResponseParam(name="msg",description="api提示信息")
     * @ResponseParam(name="data",type=array,description="返回数据")
     * @ResponseParam(name="--login_key",description="用户登录key")
     */

    public function register(){

        $type    =   $this->request()->getRequestParam('type'); //0 游客 1 QQ 2 微信 3 系统添加 4 手机号注册
        $openid  =   $this->request()->getRequestParam('openid');
        $nick    =   $this->request()->getRequestParam('name');
        $heading =   $this->request()->getRequestParam('heading');
        $sex =   $this->request()->getRequestParam('sex');   // 0:未设置 1:男;2:女

        $version = $this->appInfo['version']; //版本号
        $channel = $this->appInfo['channel']; //渠道
        $device = $this->appInfo['device'];  //设置登录设备限制
        $idfa = $this->appInfo['idfa'];      //积分墙专用

        $nick = $nick??'用户';
        $heading = $heading??'';
        $sex = $sex ?? 0;
        if(!in_array($type,[0,1,2,3,4])){
             $this->ajaxError(0,'参数错误-type');
        }
        if(empty($openid)){
            $this->ajaxError(0,'参数错误-openid');
        }

        $infoItem = $this->appInfo;
        $infoItem['name'] = $nick;
        $infoItem['head_url'] = $heading;
        $infoItem['sex'] = $sex;

        list($status,$msg,$loginKey) = \App\Repositories\Services\Member::getInstance()->login($openid,$type,$infoItem,$this->_clientIp);
        if($status == 1){
            $this->ajaxSuccess(['login_key'=>$loginKey]);
        }else{
            $this->ajaxError(0,$msg);
        }


    }


    /**
     * @Api(name="用户退出登录",group="用户",path="api/member/logout",description="退出登录")
     * @Method(allow={GET})
     * @ApiRequestExample(curl http://180.76.174.107/api/member/logout)
     * @ApiSuccess(
            {
            "code": 200,
            "msg": "OK",
            "count": 0,
            "data": []
            }
    )
     * @ResponseParam(name="code",description="状态码")
     * @ResponseParam(name="count",description="条数")
     * @ResponseParam(name="msg",description="api提示信息")
     * @ResponseParam(name="data",type=array,description="返回数据")
     * @ResponseParam(name="--login_key",description="用户登录key")
     */

    public function logout(){
        \App\Repositories\Services\Member::getInstance()->logout($this->memberItem['id']);
        $this->ajaxSuccess();

    }

    /**
     * @Api(name="用户详情",group="用户",path="api/member/info",description="用户详情")
     * @Method(allow={GET})
     * @ApiRequestExample(curl http://180.76.174.107/api/member/info)
     * @ApiSuccess(
    {
    "code": 200,
    "msg": "OK",
    "count": 0,
    "data": []
    }
    )
     * @ResponseParam(name="code",description="状态码")
     * @ResponseParam(name="count",description="条数")
     * @ResponseParam(name="msg",description="api提示信息")
     * @ResponseParam(name="data",type=array,description="返回数据")
     * @ResponseParam(name="--login_key",description="用户登录key")
     */
    public function getInfos(){
        $this->_model = new MemberInfoModel();
        $memberInfo = $this->_model->get(['id_user'=>$this->memberItem['id']]);
        $this->ajaxSuccess($memberInfo->toArray());

    }

    protected function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
        $this->_model = null;
        $this->_model_appid = null;
    }


}
